package com.heima.skydemo.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.skydemo.auto.AutoFillCreate;
import com.heima.skydemo.auto.AutoFillUpdate;
import com.heima.skydemo.domin.common.PageResult;
import com.heima.skydemo.domin.common.Result;
import com.heima.skydemo.domin.dto.EmployeeDTO;
import com.heima.skydemo.domin.dto.EmployeeLoginDTO;
import com.heima.skydemo.domin.dto.EmployeePasswordDTO;
import com.heima.skydemo.domin.dto.EmployeeQueryDTO;
import com.heima.skydemo.domin.vo.EmployeeLoginVo;
import com.heima.skydemo.domin.common.BaseContest;
import com.heima.skydemo.entity.Employee;
import com.heima.skydemo.entity.JwtProperties;
import com.heima.skydemo.mapper.EmployeeMapper;
import com.heima.skydemo.service.IEmployeeService;
import com.heima.skydemo.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-15
 */
@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JwtProperties jwtProperties;

    @PutMapping("/editPassword")
    public Result updatepossword(@RequestBody EmployeePasswordDTO employeePasswordDTO){
        log.info("修改密码：{}",employeePasswordDTO);
        employeeService.lambdaUpdate().set(Employee::getPassword,employeePasswordDTO.getNewPassword())
                .eq(Employee::getId,employeePasswordDTO.getEmpId()).update();
        return Result.success();
    }
    @PostMapping("/status/{status}")
    public Result startOrstop(@PathVariable("status") Integer status, Long id){
        log.info("启用/禁用分类状态：{}",status);
        employeeService.lambdaUpdate().set(Employee::getStatus,status).eq(Employee::getId,id).update();
        return Result.success();
    }
    @GetMapping("/page")
    public Result<PageResult> Query(EmployeeQueryDTO employeeQueryDTO){
        log.info("分页查询：{}",employeeQueryDTO);
        PageResult pageResult = employeeService.QueryPage(employeeQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/login")
    public Result<EmployeeLoginVo> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) throws NoSuchAlgorithmException {
        log.info("员工登录：{}",employeeLoginDTO);
        Employee employee = employeeMapper.selectOne(new QueryWrapper<Employee>().eq("username", employeeLoginDTO.getUsername()));
        if (employee == null){
            return Result.error("用户名不存在");
        }
        BaseContest.setContestId(employee.getId());
        Map<String, Object> claims = new HashMap<>();
        claims.put("empId", employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
        log.info("生成token:{}",token);
        EmployeeLoginVo employeeLoginVo = new EmployeeLoginVo();
        employeeLoginVo.setId(employee.getId());
        employeeLoginVo.setName(employee.getName());
        employeeLoginVo.setToken(token);
        employeeLoginVo.setUsername(employee.getUsername());

        return Result.success(employeeLoginVo);
    }
    @PostMapping("")
    @AutoFillCreate
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}",employeeDTO);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setPassword("123456");
        employeeService.save(employee);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable("id") Long id){
        log.info("根据id查询员工：{}",id);
        Employee employee = employeeMapper.selectOne(new QueryWrapper<Employee>().eq("id", id));
        return Result.success(employee);
    }
    @PutMapping("")
    @AutoFillUpdate
    @Transactional
    protected Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工：{}",employeeDTO);
        Employee employee = new Employee();
        employeeDTO.setId(employeeDTO.getId());
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setId(Long.valueOf(employeeDTO.getId()));
       employeeService.updateById(employee);
        return Result.success();
    }
    @PostMapping("logout")
    public Result logout(){
        log.info("退出登录");
        return Result.success();
    }


}
